package org.ucn.dmaa0914.gruppe6.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.ucn.dmaa0914.gruppe6.Customer;
import org.ucn.dmaa0914.gruppe6.CustomerCtr;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.FormSpecs;
import com.jgoodies.forms.layout.RowSpec;

public class CustomerView extends JPanel {

	private int customerId;
	private JTextField txtPhone;
	private JTextField txtName;
	
	/**
	 * Create the panel.
	 */
	public CustomerView() {
		setLayout(new FormLayout(new ColumnSpec[] {
				FormSpecs.RELATED_GAP_COLSPEC,
				FormSpecs.DEFAULT_COLSPEC,
				FormSpecs.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,
				FormSpecs.RELATED_GAP_ROWSPEC,
				FormSpecs.DEFAULT_ROWSPEC,}));
		
		JLabel lblName = new JLabel("Name:");
		add(lblName, "2, 2, right, default");
		
		txtName = new JTextField();
		add(txtName, "4, 2, fill, default");
		txtName.setColumns(10);
		
		JLabel lblPhone = new JLabel("Phone:");
		add(lblPhone, "2, 4, right, default");
		
		txtPhone = new JTextField();
		add(txtPhone, "4, 4, fill, default");
		txtPhone.setColumns(10);
		
		JButton btnSaveChanges = new JButton("Save Changes");
		btnSaveChanges.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CustomerCtr.getInstance().updateCustomer(customerId, txtName.getText(), txtPhone.getText());
				MainWindow.getInstance().updateViews();
			}
		});
		add(btnSaveChanges, "4, 6");

	}

	/**
	 * @param person the person to set
	 */
	public void setCustomer(int customerId) {
		this.customerId = customerId;
		
		Customer c = CustomerCtr.getInstance().find(customerId);
		if( c != null )
		{
			txtPhone.setText(c.getPhone());
			txtName.setText(c.getName());
		}
	}

}
